{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Diagrams for Course #3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/envs/py3env/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Const:0\", shape=(), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x = tf.constant(3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Const_1:0\", shape=(3,), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x = tf.constant([3, 5, 7])\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Const_2:0\", shape=(2, 3), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x = tf.constant([[3, 5, 7],\n",
    "                 [4, 6, 8]])\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Const_3:0\", shape=(2, 2, 3), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x = tf.constant([[[3, 5, 7],[4, 6, 8]],\n",
    "                 [[1, 2, 3],[4, 5, 6]]\n",
    "                 ])\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Const_4:0\", shape=(3,), dtype=int32)\n",
      "Tensor(\"stack:0\", shape=(2, 3), dtype=int32)\n",
      "Tensor(\"stack_1:0\", shape=(4, 2, 3), dtype=int32)\n",
      "Tensor(\"stack_2:0\", shape=(2, 4, 2, 3), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x1 = tf.constant([2, 3, 4])\n",
    "x2 = tf.stack([x1, x1])\n",
    "x3 = tf.stack([x2, x2, x2, x2])\n",
    "x4 = tf.stack([x3, x3])\n",
    "print(x1)\n",
    "print(x2)\n",
    "print(x3)\n",
    "print(x4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 6]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x = tf.constant([[3, 5, 7],\n",
    "                 [4, 6, 8]])\n",
    "y = x[:, 1]\n",
    "with tf.Session() as sess:\n",
    "  print(y.eval())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 5]\n",
      " [7 4]\n",
      " [6 8]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x = tf.constant([[3, 5, 7],\n",
    "                 [4, 6, 8]])\n",
    "y = tf.reshape(x, [3, 2])\n",
    "with tf.Session() as sess:\n",
    "  print(y.eval())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 4]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x = tf.constant([[3, 5, 7],\n",
    "                 [4, 6, 8]])\n",
    "y = tf.reshape(x, [3, 2])[1, :]\n",
    "with tf.Session() as sess:\n",
    "  print(y.eval())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.contrib.eager.python import tfe\n",
    "\n",
    "tfe.enable_eager_execution()\n",
    "\n",
    "x = tf.constant([[3, 5, 7],\n",
    "                 [4, 6, 8]])\n",
    "y = tf.reshape(x, [3, 2])[1, :]\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.contrib.eager.python import tfe\n",
    "\n",
    "tfe.enable_eager_execution()\n",
    "\n",
    "x = tf.constant([3, 5, 7])\n",
    "y = tf.constant([1, 2, 3])\n",
    "print(x-y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 4  7 10]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "x = tf.constant([3, 5, 7])\n",
    "y = tf.constant([1, 2, 3])\n",
    "z = tf.add(x, y)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "  print(z.eval())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 4  7 10]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "x = tf.constant([3, 5, 7])\n",
    "y = tf.constant([1, 2, 3])\n",
    "z = tf.add(x, y)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "  print(sess.run(z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 4  7 10]\n",
      "[-1  3 11]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "x = tf.constant([3, 5, 7])\n",
    "y = tf.constant([1, 2, 3])\n",
    "z1 = x + y\n",
    "z2 = x * y\n",
    "z3 = z2 - z1\n",
    "\n",
    "with tf.Session() as sess:\n",
    "  a1, a3 = sess.run([z1, z3])\n",
    "  print(a1)\n",
    "  print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "x = tf.constant([3, 5, 7], name=\"x\")\n",
    "y = tf.constant([1, 2, 3], name=\"y\")\n",
    "z1 = tf.add(x, y, name=\"z1\")\n",
    "z2 = x * y\n",
    "z3 = z2 - z1\n",
    "\n",
    "with tf.Session() as sess:\n",
    "  with tf.summary.FileWriter('summaries', sess.graph) as writer:\n",
    "      a1, a3 = sess.run([z1, z3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "events.out.tfevents.1536178690.b7bd1d29df22\r\n",
      "events.out.tfevents.1536178873.b7bd1d29df22\r\n"
     ]
    }
   ],
   "source": [
    "!ls summaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>TensorBoard was started successfully with pid 18325. Click <a href=\"/_proxy/36513/\" target=\"_blank\">here</a> to access it.</p>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "18325"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from google.datalab.ml import TensorBoard\n",
    "TensorBoard().start('./summaries')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stopped TensorBoard\n"
     ]
    }
   ],
   "source": [
    "from google.datalab.ml import TensorBoard\n",
    "TensorBoard().stop(13045)\n",
    "print('stopped TensorBoard')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:[[ 8.568541 12.702375 17.271353]]\n",
      "1:[[ 9.318541  13.8323765 18.821354 ]]\n",
      "2:[[10.068541 14.962376 20.371353]]\n",
      "3:[[10.818541 16.092377 21.921354]]\n",
      "4:[[11.56854  17.222376 23.471352]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "def forward_pass(w, x):\n",
    "    return tf.matmul(w, x)\n",
    "\n",
    "def train_loop(x, niter=5):\n",
    "  with tf.variable_scope(\"model\", reuse=tf.AUTO_REUSE):\n",
    "    w = tf.get_variable(\"weights\", \n",
    "                    shape=(1,2),  # 1 x 2 matrix\n",
    "                    initializer=tf.truncated_normal_initializer(),\n",
    "                    trainable=True)\n",
    "  preds = []\n",
    "  for k in range(niter):\n",
    "    preds.append(forward_pass(w, x))\n",
    "    w = w + 0.1 # \"gradient update\"\n",
    "  return preds\n",
    "\n",
    "with tf.Session() as sess:\n",
    "  preds = train_loop(tf.constant([[3.2, 5.1, 7.2],[4.3, 6.2, 8.3]])) # 2 x 3 matrix\n",
    "  tf.global_variables_initializer().run()\n",
    "  for i in range(len(preds)):\n",
    "      print(\"{}:{}\".format( i, preds[i].eval() ))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
